package com.gel.aiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.gel.aiagent.contant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
/*读写文件工具*/
public class FileOperationTool {
    private  final String FILE_DIR= FileConstant.FILE_SAVE_DIR;


    /*读取文件*/
    @Tool(description = "read content from a file")
    public String readFile(@ToolParam(description = "Name of file to read") String fileName) {
        String filePath=FILE_DIR+"/"+fileName;
        try{
            return FileUtil.readUtf8String(filePath);
        }catch (Exception e){
            return "read file failed"+e.getMessage();
        }

    }

    /*写入文件*/

    @Tool(description = "write content to a file")
    public String writeFile(@ToolParam(description = "Name of file to write") String fileName,
                            @ToolParam(description = "Content to write to the file") String content){
        String filePath=FILE_DIR+"/"+fileName;
        try{
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content,filePath);
            return "file witten successfully to"+filePath;
        }catch (Exception e){
            return "file witten failed"+e.getMessage();
        }
    }
}
